% \iffalse meta-comment
%<*internal>
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
mynsfc --- A LaTeX template for writing the main body of NSFC proposals.
Author:  Fei Qi
E-mail:  fred.qi@ieee.org
License: Released under the LaTeX Project Public License v1.3c or later
See:     http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
\endpreamble
\postamble

Copyright (C) 2015-2018 by Fei Qi <fred.qi@ieee.org>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
Fei Qi.

This work consists of the file mynsfc.dtx and a Makefile.
Running "make" generates the derived files README, mynsfc.pdf and mynsfc.cls.
Running "make inst" installs the files in the user's TeX tree.
Running "make install" installs the files in the local TeX tree.

\endpostamble

\usedir{tex/latex/mynsfc}
\generate{
  \file{\jobname.cls}{\from{\jobname.dtx}{class}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/mynsfc}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{mynsfc.dtx}
%</driver>
%<class>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<class>\ProvidesClass{mynsfc}
%<*class>
    [2018/08/05 v1.20 A LuaLaTeX class for writing NSFC proposals.]
%</class>
%<*driver>
\documentclass{ltxdoc}
\usepackage[a4paper,margin=25mm,left=50mm,nohead]{geometry}
\usepackage[numbered]{hypdoc}
\usepackage[UTF8,fontset=fandol,heading=true,12pt]{ctex}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{\jobname.dtx}
% \DoNotIndex{\newcommand,\newenvironment}
%
% \title{\textsf{mynsfc} --- 国家自然科学基金申请书正文模板\thanks{此文档描述的
% 模板版本为 \fileversion, 最后修改日期为
% \filedate.} } \author{Fred Qi\thanks{E-mail: fred.qi@ieee.org}} \date{
% \filedate 发布}
%
% \maketitle
%
% \changes{v1.00}{2015/08/18}{First public release}
% \changes{v1.01}{2016/07/11}{Improved command \texttt{maketitle}}
% \changes{v1.01}{2016/07/11}{Added an option \texttt{arabicpart}}
% \changes{v1.01}{2016/07/11}{Author highlight with \texttt{biblatex} newer than
% 3.3 (2016-03-01)}
% \changes{v1.01}{2016/09/05}{Added options \texttt{tocfont} and
% \texttt{boldtoc} to select font on headings}
% \changes{v1.01}{2016/09/05}{Using kvoptions to handle package options}
% \changes{v1.01}{2016/09/05}{Added an option \texttt{toccolor} to show outline
% in a given color specified by a HTML/CSS style hex code}
% \changes{v1.20}{2018/08/05}{Changed to support Chinese based on the CTeX package.}
% \changes{v1.21}{2018/08/22}{Using xelatex for the FakeBold feature.}
% 
% \begin{abstract}
% 用于自然基金申请书正文部分的撰写。
% \end{abstract}
%
% \section{使用说明}
%
% 参见样例文件 \texttt{examples/my-nsfc-proposal.tex}。
%
%\StopEventually{^^A
%  \PrintChanges
%  \PrintIndex
%}
%
% \section{实现}
%
%<*class>
%    \begin{macrocode}
\ExecuteOptions{}
\ProcessOptions*
%% Options
\RequirePackage{kvoptions}
\DeclareBoolOption[false]{subfig}
\DeclareBoolOption[false]{boldtoc}
\DeclareStringOption[zhkai]{tocfont}
\DeclareStringOption[0070c0]{toccolor}
\ProcessKeyvalOptions*
%% Load default class
\LoadClass[a4paper,UTF8,fontset=fandol,zihao=-4]{ctexart}
%    \end{macrocode}
%
%    \begin{macrocode}
%% Load required packages
\RequirePackage{titlesec}
\RequirePackage{marvosym}
\RequirePackage{amsmath,amssymb}
\RequirePackage{paralist}
\RequirePackage{graphicx}
\ifmynsfc@subfig
\RequirePackage[config]{subfig}
\else
\RequirePackage{caption,subcaption}
\fi
\RequirePackage{xcolor}
\RequirePackage{calc}
\RequirePackage{hyperref}
\hypersetup{%
  breaklinks=true,
  colorlinks=true,
  allcolors=black,
  pdfpagelabels}
\urlstyle{same}
%    \end{macrocode}
%    \begin{macrocode}
%% Load and setup package biblatex
\RequirePackage[backend=biber,
                url=true,
                isbn=false,
                defernumbers=true,
                style=ieee]{biblatex}
\appto{\bibbont}{\zihao{5}}
\defbibheading{reftype}[\bibname]{\subsection*{#1}}
\defbibheading{cvtype}[\bibname]{\paragraph{#1}}
\defbibfilter{conference}{type=inproceedings or type=incollection}

\RequirePackage{xpatch}% or use http://tex.stackexchange.com/a/40705

\@ifpackagelater{biblatex}{2016/03/01}
{
\newcommand*{\list@bold@authors}{}
\newcommand{\initauthors}[1]{
  \renewcommand*{\list@bold@authors}{}
  \forcsvlist{\listadd\list@bold@authors}{#1}}

\newboolean{bold}
\renewcommand*{\mkbibnamefamily}[1]{\ifthenelse{\boolean{bold}}{\textbf{#1}}{#1}}
\renewcommand*{\mkbibnamegiven}[1]{\ifthenelse{\boolean{bold}}{\textbf{#1}}{#1}}

\newbibmacro*{name:bold}{%
  \setboolean{bold}{false}%
  \def\do##1{\iffieldequalstr{hash}{##1}{\setboolean{bold}{true}\listbreak}{}}%
  \dolistloop{\list@bold@authors}%
}

\xpretobibmacro{name:family}{\begingroup\usebibmacro{name:bold}}{}{}{}{}
\xpretobibmacro{name:given-family}{\begingroup\usebibmacro{name:bold}}{}{}{}{}
\xpretobibmacro{name:family-given}{\begingroup\usebibmacro{name:bold}}{}
%\xpretobibmacro{name:delim}{\begingroup\normalfont}{}{}

\xapptobibmacro{name:family}{\endgroup}{}{}{}{}
\xapptobibmacro{name:given-family}{\endgroup}{}{}{}{}
\xapptobibmacro{name:family-given}{\endgroup}{}{}{}{}
%\xapptobibmacro{name:delim}{\endgroup}{}{}
}
{
\newbibmacro*{name:bold}[2]{%
  \def\do##1{\ifstrequal{#1, #2}{##1}{\bfseries\listbreak}{}}%
  \dolistloop{\boldnames}}
\newcommand*{\boldnames}{}

\xpretobibmacro{name:last}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:first-last}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:last-first}{\begingroup\usebibmacro{name:bold}{#1}{#2}}{}{}
\xpretobibmacro{name:delim}{\begingroup\normalfont}{}{}

\xapptobibmacro{name:last}{\endgroup}{}{}
\xapptobibmacro{name:first-last}{\endgroup}{}{}
\xapptobibmacro{name:last-first}{\endgroup}{}{}
\xapptobibmacro{name:delim}{\endgroup}{}{}
}
%    \end{macrocode}
%
% \begin{macro}{\tocformat}
%    \begin{macrocode}
\newcommand{\tocformat}{%
  \CJKfamily{\mynsfc@tocfont}%
  \color[HTML]{\mynsfc@toccolor}}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
% Define page layout
\setlength{\textwidth}{\paperwidth}
\setlength{\textheight}{\paperheight}
\setlength\marginparwidth{0mm}
\setlength\marginparsep{0mm}
\addtolength{\textwidth}{-50mm}
\setlength{\oddsidemargin}{0mm}
\setlength{\evensidemargin}{\oddsidemargin}
\setlength{\headheight}{20pt}
\setlength{\topskip}{0mm}
\setlength{\skip\footins}{15pt}
\setlength{\topmargin}{-15mm}
\setlength{\footskip}{13mm}
\setlength{\headsep}{6mm}
\addtolength{\textheight}{-50mm}
\setlength{\parskip}{0pt \@plus2pt \@minus0pt}
%    \end{macrocode}
%    \begin{macrocode}
% Define page styles      
\def\ps@mynsfc@empty{%
  \let\@oddhead\@empty%
  \let\@evenhead\@empty%
  \let\@oddfoot\@empty%
  \let\@evenfoot\@empty}
%    \end{macrocode}
%    \begin{macrocode}
\setCJKmainfont[AutoFakeBold=2]{FandolKai-Regular.otf}
\setCJKfamilyfont{zhkai}[AutoFakeBold=2]{FandolKai-Regular.otf}
%    \end{macrocode}
% \begin{macro}{\maketitle}
\renewcommand{\maketitle}{%
  \begin{center}%
    \kaishu\zihao{2}\@title%
  \end{center}}
% \end{macro}
%
%    \begin{macrocode}
\ctexset{
  part/name         = {（,）},
  part/aftername    = {},
  part/number       = \chinese{part},
  part/format       = \tocformat\bfseries\zihao{4},
  part/indent       = \parindent,
}
\titleformat{\section}[block]{\tocformat\zihao{4}}
                             {\bfseries\hskip2em\thesection{.}}{1ex}{}
\titlespacing{\section}{0em}{4ex}{2ex}
\titleformat{\subsection}{\tocformat\bfseries\zihao{-4}}
                         {\thesubsection{.}}{0.25em}{}
\titlespacing{\subsection}{0em}{2ex}{1ex}
\titleformat{\subsubsection}{\tocformat\bfseries\zihao{-4}}
                            {\thesubsubsection{.}}{0.25em}{}
\titlespacing{\subsubsection}{0em}{2ex}{1ex}
\let\oldsection\section
\renewcommand{\section}[2]{\oldsection{\textbf{#1}{#2}}}
\@addtoreset{section}{part}
\captionsetup{font=small}
%    \end{macrocode}
%    \begin{macrocode}
\let\mynsfc@begindocumenthook\@begindocumenthook
\let\mynsfc@enddocumenthook\@enddocumenthook
\def\AtBeginDocument{\g@addto@macro\mynsfc@begindocumenthook}
\def\AtEndDocument{\g@addto@macro\mynsfc@enddocumenthook}
\def\@begindocumenthook{\mynsfc@begindocumenthook}
\def\@enddocumenthook{\mynsfc@enddocumenthook}
\AtBeginDocument{\ps@mynsfc@empty}
\endinput
%</class>
%    \end{macrocode}
%\Finale
